angular.module('start.common.login.ctrl', [])

  .controller('LoginCtrl', function($scope, $rootScope, $$AuthService, $location, $$UserDataService, $ionicLoading) {
    console.log("LoginCtrl");

    $scope.$on("$ionicView.beforeEnter", function() { //每次进入页面前判断是否登录
      console.log("$ionicView.beforeEnter------LoginCtrl");
      //判断登录代码
      // $ionicHistory.clearCache() ;
      if ($rootScope.config.isLogined) {
        $location.path("tab/user");
      }
    });

    $scope.loginData = {
      telephone: "",
      password: ""
    };

    // 登录按钮
    $scope.login = function() {
      // 手机校验
      if (!checkPhone($scope.loginData.telephone)) {
        $ionicLoading.show({
          template: '请输入正确的手机号码',
          noBackdrop: true,
          duration: 1500
        });
        return; //不通过就不执行下一步
      }
      // 密码校验
      if (!checkPassword($scope.loginData.password)) {
        $ionicLoading.show({
          template: '请输入正确6~12位的密码',
          noBackdrop: true,
          duration: 1500
        });
        return;
      }

      // 登录请求
      $$AuthService.login($scope.loginData.telephone, $scope.loginData.password)
        .then(function(res) {
          $rootScope.userData = res.data;
          $rootScope.userData.token = res.token;

          $$UserDataService.updateUserData(res.data);
          $rootScope.isLogined();
          $ionicLoading.show({
            template: "登录成功",
            noBackdrop: true,
            duration: 1000
          });
          // // 如果没有上一页的话自动跳转到 用户中心
          if ($rootScope.config.backPath == "") {
            $location.path("tab/user");
          } else {
            // 如果是用户访问的页面需要登录 那么登录成功后跳转到的用户想访问的页面
            $location.path($rootScope.config.backPath);
            $rootScope.config.backPath = "";
            $rootScope.config.backPathNologin = "";
          }
        });

    };


    $scope.wechatLogin = function(argument) {
        $ionicLoading.show({
          noBackdrop: true,
          duration: 5000
        });
      document.addEventListener("deviceready", function() {
        var scope = "snsapi_userinfo",
          state = "_" + (+new Date());
        Wechat.auth(scope, state, function(response) {
          // you may use response.code to get the access token.
          // alert(JSON.stringify(response));
          $$AuthService.weixinloginsendCode(response.code)
            .then(function(res) {
              // alert(JSON.stringify(res));
              if (res.data.openid != null && res.data.openid != undefined) {
                $rootScope.config.thirdpartLogin.way = "wechat";
                $rootScope.config.thirdpartLogin.openid = res.data.openid;
                $location.path("registerthirdpart");
              } else {
                $rootScope.userData = res.data;
                $rootScope.userData.token = res.token;
                $$UserDataService.updateUserData(res.data);
                $rootScope.isLogined();
                $ionicLoading.show({
                  template: "登录成功",
                  noBackdrop: true,
                  duration: 1000
                });
                // // 如果没有上一页的话自动跳转到 用户中心
                if ($rootScope.config.backPath == "") {
                  $location.path("tab/user");
                } else {
                  // 如果是用户访问的页面需要登录 那么登录成功后跳转到的用户想访问的页面
                  $location.path($rootScope.config.backPath);
                  $rootScope.config.backPath = "";
                  $rootScope.config.backPathNologin = "";
                }
              }
              $ionicLoading.hide();
            });
        }, function(reason) {
          // alert("Failed: " + reason);
          $ionicLoading.hide();
        });
        $ionicLoading.show({
          noBackdrop: true,
        });
      });
    };

    $scope.weiboLogin = function(argument) {
      // document.addEventListener("deviceready", function() {
      //   WeiboSDK.ssoLogin(function(args) {
      //     alert(JSON.stringify(args));
      //     $ionicLoading.hide();
      //     // alert('access token is ' + args.access_token);
      //     // alert('userId is ' + args.userId);
      //     // alert('expires_time is ' + new Date(parseInt(args.expires_time)) + ' TimeStamp is ' + args.expires_time);
      //   }, function(failReason) {
      //     alert(failReason);
      //     $ionicLoading.hide();
      //   });
      //   $ionicLoading.show({
      //     noBackdrop: true,
      //     duration: 10000
      //   });
      // });
    };

    $scope.qqLogin = function(argument) {
      // document.addEventListener("deviceready", function() {
      //   var args = {};
      //   args.client = QQSDK.ClientType.QQ; //QQSDK.ClientType.QQ,QQSDK.ClientType.TIM;
      //   QQSDK.ssoLogin(function(result) {
      //     alert(JSON.stringify(result));
      //     $ionicLoading.hide();
      //     // alert('token is ' + result.access_token);
      //     // alert('userid is ' + result.userid);
      //     // alert('expires_time is ' + new Date(parseInt(result.expires_time)) + ' TimeStamp is ' + result.expires_time);
      //   }, function(failReason) {
      //     alert(failReason);
      //     $ionicLoading.hide();
      //   }, args);
      //   $ionicLoading.show({
      //     noBackdrop: true,
      //     duration: 10000
      //   });
      // });
    };


    // 登录页的返回按钮
    $scope.goBackPageLogin = function() {
      // if ($rootScope.config.backPathNologin == "") {
      //   $rootScope.goBackPage();
      // } else {
      //   if ($rootScope.isLogined() == true) {
      //     $rootScope.goBackPage();
      //   } else {
      //     $location.path($rootScope.config.backPathNologin);
      //     $rootScope.config.backPathNologin = "";
      //   }
      // }
    };



  });
